#Created in Feb 2,2022
#Author:Char_X
#Last edited:May 23,2022(OK)

TARGET=HT32
TARGET_NAME=UART

vpath %.c ../HTSources
vpath %.c ../CMSIS/
vpath %.c ../HT32_Board
vpath %.c ../common
vpath %.c ../Driver/AS5047P
vpath %.c ../Driver/BLDC
vpath %.c ../Driver/Bluetooth
vpath %.c ../Driver/Hard_SPI
vpath %.c ../Driver/PWM
vpath %.c ../Driver/Timer
vpath %.c ../Driver/USART
vpath %.c ../


ARMGCC=arm-none-eabi
ARMCC=$(ARMGCC)-gcc
ARMAR=$(ARMGCC)-ar
ARMAS=$(ARMGCC)-as
ARMLD=$(ARMGCC)-ld
ARMOBJCOPY=$(ARMGCC)-objcopy
ARMOBJDUMP=$(ARMGCC)-objdump

ARM_CORE=cortex-m0
ARCH_OPTION=-mcpu=$(ARM_CORE) -mthumb -mthumb-interwork
CC_OPTION=-gdwarf-2 -MD -Wall -Os -mapcs-frame -ffunction-sections -fdata-sections
LD_OPTION=$(ARCH_OPTION) -Wl,-Map=$(TARGET_NAME).map -Wl,--gc-sections --specs=nano.specs

CC_OPTION += -DUSE_HT32_DRIVER
CC_OPTION += -DUSE_HT32F52352_SK
CC_OPTION += -DUSE_HT32F52342_52
CC_OPTION += -DUSE_MEM_HT32F52352
AS_OPTION = --defsym USE_HT32_CHIP=4

LINK_NAME = ../linker.ld
START_NAME_PATH = startup_ht32f5xxxx_gcc_01
STARTUP_PATH = ../Startup/
CC_OBJECT_OPTION += $(ARCH_OPTION) $(CC_OPTION) -c

#---------------------------------------------------------
#----------------------INCLUDE_PATH-----------------------
#---------------------------------------------------------
INCLUDE_PATH += -I../HTLibraries/
INCLUDE_PATH += -I../CMSIS/
INCLUDE_PATH += -I../HT32_Board/
INCLUDE_PATH += -I../common/
INCLUDE_PATH += -I../
INCLUDE_PATH += -I../Driver/AS5047P
INCLUDE_PATH += -I../Driver/BLDC
INCLUDE_PATH += -I../Driver/Bluetooth
INCLUDE_PATH += -I../Driver/Hard_SPI
INCLUDE_PATH += -I../Driver/PWM
INCLUDE_PATH += -I../Driver/Timer
INCLUDE_PATH += -I../Driver/USART

ASM_OBJECT_OPTION = $(ARCH_OPTION) $(AS_OPTION)

HEX_OPTION = -O ihex
BIN_OPTION = -O binary
TEXT_OPTION = -D

#---------------------------------------------------------
#----------------------OBJ_PATH------------------------
#---------------------------------------------------------
#HTDriver
SOURCE_PATH += system_ht32f5xxxx_01.o
SOURCE_PATH += \
ht32_cm0plus_misc.o \
ht32f5xxxx_adc.o \
ht32f5xxxx_bftm.o \
ht32f5xxxx_ckcu.o \
ht32f5xxxx_cmp.o \
ht32f5xxxx_crc.o \
ht32f5xxxx_ebi.o \
ht32f5xxxx_exti.o \
ht32f5xxxx_flash.o \
ht32f5xxxx_gpio.o \
ht32f5xxxx_i2c.o \
ht32f5xxxx_i2s.o \
ht32f5xxxx_mctm.o \
ht32f5xxxx_pdma.o \
ht32f5xxxx_pwrcu.o \
ht32f5xxxx_rstcu.o \
ht32f5xxxx_rtc.o \
ht32f5xxxx_sci.o \
ht32f5xxxx_spi.o \
ht32f5xxxx_tm.o \
ht32f5xxxx_usart.o \
ht32f5xxxx_usbd.o \
ht32f5xxxx_wdt.o \
ht32_retarget.o \
ht32_serial.o \
printf.o \
syscalls.o \
ht32_board.o \
ebi_lcd.o \
i2c_eeprom.o \
spi_flash.o \
#ht32_usbd_core.o \
#UserDriver
SOURCE_PATH += \
main.o \


build:$(TARGET_NAME).elf $(TARGET_NAME).hex $(TARGET_NAME).bin
	@echo 
	@echo Build Successfully!
$(TARGET_NAME).elf:$(SOURCE_PATH) $(START_NAME_PATH).o
	$(ARMCC) -T$(LINK_NAME) $(LD_OPTION) $^ -o $(TARGET_NAME).elf
$(TARGET_NAME).hex:$(TARGET_NAME).elf
	$(ARMOBJCOPY) $(HEX_OPTION) $(TARGET_NAME).elf $(TARGET_NAME).hex
$(TARGET_NAME).bin:$(TARGET_NAME).hex
	$(ARMOBJCOPY) $(BIN_OPTION) $(TARGET_NAME).elf $(TARGET_NAME).bin

$(START_NAME_PATH).o:$(STARTUP_PATH)$(START_NAME_PATH).s
	$(ARMAS) $(ASM_OBJECT_OPTION) $(STARTUP_PATH)$(START_NAME_PATH).s -o $(START_NAME_PATH).o

clean:
	@echo Cleaning
	del $(SOURCE_PATH) $(START_NAME_PATH).o $(TARGET_NAME).hex $(TARGET_NAME).elf $(TARGET_NAME).bin *.d

rebuild:clean build

%.o:%.c
	$(ARMCC) $(CC_OBJECT_OPTION) $(INCLUDE_PATH) $^